﻿package com.webdesign.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webdesign.dao.jdbc.TbHomeworkSQL;
import com.webdesign.dao.mapper.TbDisplyMapper;
import com.webdesign.dao.mapper.TbHomeworkMapper;
import com.webdesign.dao.mapper.TbUserMapper;
import com.webdesign.dao.mapper.TbUserhomeworkMapper;
import com.webdesign.dto.HomeworkInfoDTO;
import com.webdesign.dto.ModifyHomeworkDTO;
import com.webdesign.entity.TbDisply;
import com.webdesign.entity.TbHomework;
import com.webdesign.entity.TbHomeworkExample;
import com.webdesign.entity.TbHomeworkExample.Criteria;
import com.webdesign.entity.TbUserExample;
import com.webdesign.entity.TbUserhomework;
import com.webdesign.entity.TbUserhomeworkExample;
import com.webdesign.service.ITbHomeworkService;
import com.webdesign.util.TimeChangeUtil;

@Service("TbHomeworkService")
public class TbHomeworkService implements ITbHomeworkService {

	@Autowired
	private TbHomeworkMapper tbHomeworkMapper;
	
	@Autowired
	private TbUserMapper tbUserMapper;
	
	@Autowired
	private TbUserhomeworkMapper tbUserhomeworkMapper;
	
	@Autowired
	private TbDisplyMapper tbDisplyMapper;

	/*
	 * 根据学年id获取该学年的所有作业的信息,已完成和未完成情况
	 */
	@Override
	public List<HomeworkInfoDTO> showHomework(String termid,String teacherid) {
		
		//根据termid查询特别homework表格，获取该学年发布的所有作业
		TbHomeworkExample tbHomeworkExample = new TbHomeworkExample();
		tbHomeworkExample.createCriteria().andTermidEqualTo(termid).andTeacheridEqualTo(teacherid);
		tbHomeworkExample.setOrderByClause("CreateTime desc");
		List<TbHomework> tbHomeworkList = tbHomeworkMapper.selectByExample(tbHomeworkExample);
		
		//根据termid+teacherid获取该学年该老师的所有学生人数
		TbUserExample tbUserExample = new TbUserExample();
		tbUserExample.createCriteria().andTermidEqualTo(termid).andTeacheridEqualTo(teacherid);
		int userNum = tbUserMapper.countByExample(tbUserExample);

		List<HomeworkInfoDTO> homeworkList = new ArrayList<HomeworkInfoDTO>();
		//循环每一个作业，判断该作业对应的已完成和未完成情况
		for (TbHomework tbHomework : tbHomeworkList) {
			//用于存放每个作业对应的信息，以及已完成/未完成情况
			HomeworkInfoDTO homework = new HomeworkInfoDTO();

			//根据homeworkid去tbuserhomework表格统计状态为1或2的作业条数为已完成作业的学生人数
			TbUserhomeworkExample tbUserhomeworkExample = new TbUserhomeworkExample();
			tbUserhomeworkExample.createCriteria().andHomeworkidEqualTo(tbHomework.getHomeworkid());
			int submitnum = tbUserhomeworkMapper.countByExample(tbUserhomeworkExample);
			
			
			homework.setHomeworkid(tbHomework.getHomeworkid());
			homework.setTitle(tbHomework.getTitle());
			homework.setDescription(tbHomework.getDescription());
			homework.setGraded(tbHomework.getGraded());
			homework.setTermid(tbHomework.getTermid());
			homework.setStarttime(tbHomework.getStarttime());
			homework.setEndtime(tbHomework.getEndtime());
			homework.setCreatetime(tbHomework.getCreatetime());
			homework.setSubmitnum(submitnum);
			//未完成人数即为该学年总人数减去已完成人数
			homework.setUnsubmitnum(userNum-submitnum);
			homework.setState();

			homeworkList.add(homework);
		}
		return homeworkList;
	}

	/*
	 *老师发布作业
	 */
	@Override
	public Boolean submitHomework(String title, String description,
			String starttime, String endtime, String graded, String termid,String teacherid) {

		/*
		 * 将dd/mm/yyyy转化成DATE格式
		 */
		Date start = new Date();
		Date end = new Date();
		try {
			start = TimeChangeUtil.toToday(TimeChangeUtil
					.stringChange(starttime));
			end = TimeChangeUtil.toToday(TimeChangeUtil.stringChange(endtime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		TbHomeworkSQL tbHomeworkSQL = new TbHomeworkSQL();
		TbHomework tbHomework = new TbHomework();
		String ii = tbHomeworkSQL.getMaxHomeworkid();
		String id;
		if (ii == null) {
			id = termid + "_" + "1";
		} else {
			String str[] = ii.split("_");
			id = termid + "_" + String.valueOf(Integer.parseInt(str[1]) + 1);
		}
		tbHomework.setHomeworkid(id);
		tbHomework.setTitle(title);
		tbHomework.setDescription(description);
		tbHomework.setTermid(termid);
		tbHomework.setStarttime(start);
		tbHomework.setEndtime(end);
		tbHomework.setCreatetime(new Date());
		tbHomework.setTeacherid(teacherid);

		if (tbHomeworkMapper.insertSelective(tbHomework) == 1) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 学生查看作业详情
	 * homeworkid,作业号
	 * 
	 */
	@Override
public HomeworkInfoDTO checkDetail(String homeworkid){		
		TbHomework tbHomework = tbHomeworkMapper.selectByPrimaryKey(homeworkid);
		HomeworkInfoDTO homeworkInfoDTO = new HomeworkInfoDTO();
		homeworkInfoDTO.setTitle(tbHomework.getTitle());
		homeworkInfoDTO.setDescription(tbHomework.getDescription());
		homeworkInfoDTO.setEndtime(tbHomework.getEndtime());			
		homeworkInfoDTO.setGraded(tbHomework.getGraded());			
		homeworkInfoDTO.setStarttime(tbHomework.getStarttime());
		
		return homeworkInfoDTO;	
}	

	/*
	 *根据作业id返回作业详情信息（由于返回的起止时间的格式问题区别于checkDetail接口） 
	 */
	@Override
	public ModifyHomeworkDTO getHomeworkinfo(String homeworkid) {
		TbHomework tbHomework = tbHomeworkMapper.selectByPrimaryKey(homeworkid);
		ModifyHomeworkDTO modifyHomeworkDTO = new ModifyHomeworkDTO();
		TimeChangeUtil timechange = new TimeChangeUtil();

		modifyHomeworkDTO.setHomeworkid(tbHomework.getHomeworkid());
		modifyHomeworkDTO.setTitle(tbHomework.getTitle());
		modifyHomeworkDTO.setGraded(tbHomework.getGraded());
		modifyHomeworkDTO.setDescription(tbHomework.getDescription());
		modifyHomeworkDTO.setStarttime(timechange.changeToString(tbHomework.getStarttime()));
		modifyHomeworkDTO.setEndtime(timechange.changeToString(tbHomework.getEndtime()));
		return modifyHomeworkDTO;
	}

	/*
	 * 老师修改已经发布的作业的详情信息
	 */
	@Override
	public Boolean modifyHomework(String homeworkid, String title,
			String description, String starttime, String endtime,
			String graded) {
		TbHomework homework = tbHomeworkMapper.selectByPrimaryKey(homeworkid);
		/*
		 * 将dd/mm/yyyy转化成DATE格式
		 */
		Date start = new Date();
		Date end = new Date();
		try {
			start = TimeChangeUtil.toToday(TimeChangeUtil
					.stringChange(starttime));
			end = TimeChangeUtil.toToday(TimeChangeUtil.stringChange(endtime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		TbHomework tbHomework = new TbHomework();
		
		tbHomework.setHomeworkid(homeworkid);
		tbHomework.setTitle(title);
		tbHomework.setDescription(description);
		tbHomework.setStarttime(start);
		tbHomework.setEndtime(end);
		tbHomework.setGraded(Byte.parseByte(graded));
		tbHomework.setTermid(homework.getTermid());
		tbHomework.setCreatetime(homework.getCreatetime());
		tbHomework.setTeacherid(homework.getTeacherid());
		

		if (tbHomeworkMapper.updateByPrimaryKey(tbHomework) == 1) {
			return true;
		} else {
			return false;
		}

	}
	
	/*
	 * 根据作业编号删除作业信息
	 */
	@Override
	public Boolean delateHomework(String homeworkid) {
		
		if(tbHomeworkMapper.deleteByPrimaryKey(homeworkid) == 1){
			TbUserhomeworkExample tbUserhomeworkExample = new TbUserhomeworkExample();
			tbUserhomeworkExample.createCriteria().andHomeworkidEqualTo(homeworkid);
			List <TbUserhomework> tbUserhomeworks = tbUserhomeworkMapper.selectByExample(tbUserhomeworkExample);
			if(!tbUserhomeworks.isEmpty()){
				for(TbUserhomework tbuserhomework : tbUserhomeworks){
					TbDisply tbDisply = tbDisplyMapper.selectByPrimaryKey(tbuserhomework.getUserhomeworkid());
					if(tbDisply != null){
						if(tbDisplyMapper.deleteByPrimaryKey(tbuserhomework.getUserhomeworkid()) == 1){
							
						}else{
							return false;
						}
						
					}
				}
				if(tbUserhomeworkMapper.deleteByExample(tbUserhomeworkExample)==1){
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
		}
		return false;
	}

	/*
	 * 管理员查看所有作业信息
	 */
	public List<HomeworkInfoDTO> showAllHomeworks(){
		List<HomeworkInfoDTO> homeworkInfoDTOs = new ArrayList<HomeworkInfoDTO>();
		
		TbHomeworkExample tbHomeworkExample = new TbHomeworkExample();
		tbHomeworkExample.createCriteria().getAllCriteria();
		List<TbHomework> homeworks = tbHomeworkMapper.selectByExample(tbHomeworkExample);
		
		TbUserExample tbUserExample = new TbUserExample();
		TbUserhomeworkExample tbUserhomeworkExample = new TbUserhomeworkExample();
		int studentNum = 0;
		int finishNum = 0;
		HomeworkInfoDTO homeworkInfoDTO;
		for(TbHomework tbHomework:homeworks){
			tbUserExample.clear();
			
			tbUserExample.createCriteria().andTermidEqualTo(tbHomework.getTermid());
			studentNum = tbUserMapper.countByExample(tbUserExample);
			
			tbUserhomeworkExample.clear();
			tbUserhomeworkExample.createCriteria().andHomeworkidEqualTo(tbHomework.getHomeworkid());
			finishNum = tbUserhomeworkMapper.countByExample(tbUserhomeworkExample);
			
			homeworkInfoDTO = new HomeworkInfoDTO();
			homeworkInfoDTO.setTitle(tbHomework.getTitle());
			homeworkInfoDTO.setDescription(tbHomework.getDescription());
			homeworkInfoDTO.setSubmitnum(finishNum);
			homeworkInfoDTO.setUnsubmitnum(studentNum - finishNum);
			
			homeworkInfoDTOs.add(homeworkInfoDTO);
		}
		
		return homeworkInfoDTOs;
		
	}

	@Override
	public List<HomeworkInfoDTO> checkHomeworks(String teacherid, String termid) {
		// TODO Auto-generated method stub
				//根据username和termname查询homework表格，获取该教师该学年发布的所有作业
				List<HomeworkInfoDTO> homeworkInfoDTOs = new ArrayList<HomeworkInfoDTO>();
				
				TbHomeworkExample tbHomeworkExample = new TbHomeworkExample();
				 Criteria c = tbHomeworkExample.createCriteria();
				if(!teacherid .equals("")){
					c.andTeacheridEqualTo(teacherid);
				}
				if(!termid.equals("")){
					c.andTermidEqualTo(termid);
				}
				
				tbHomeworkExample.setOrderByClause("CreateTime desc");
				List<TbHomework> homeworks = tbHomeworkMapper.selectByExample(tbHomeworkExample);
				
				TbUserExample tbUserExample = new TbUserExample();
				TbUserhomeworkExample tbUserhomeworkExample = new TbUserhomeworkExample();
				int studentNum = 0;
				int finishNum = 0;
				HomeworkInfoDTO homeworkInfoDTO;
				
				for(TbHomework tbHomework:homeworks){
					tbUserExample.clear();
					tbUserExample.createCriteria().andTermidEqualTo(tbHomework.getTermid());
					studentNum = tbUserMapper.countByExample(tbUserExample);
					
					tbUserhomeworkExample.clear();
					tbUserhomeworkExample.createCriteria().andHomeworkidEqualTo(tbHomework.getHomeworkid());
					finishNum = tbUserhomeworkMapper.countByExample(tbUserhomeworkExample);
					
					homeworkInfoDTO = new HomeworkInfoDTO();
					homeworkInfoDTO.setTitle(tbHomework.getTitle());
					homeworkInfoDTO.setDescription(tbHomework.getDescription());
					homeworkInfoDTO.setSubmitnum(finishNum);
					homeworkInfoDTO.setUnsubmitnum(studentNum - finishNum);
					
					homeworkInfoDTOs.add(homeworkInfoDTO);
				}
				return homeworkInfoDTOs;
	}

}
